<?php
include_once dirname(__FILE__) . '/main.class.php';

class Costi extends Main
{
	public function GetIdTariffa($tipologia, $durata, $tipo)
	{
		try
		{
			$id_tariffa = 0;
			if($tipo=="Banner")
				$sql = "SELECT id_tariffa from tariffe_banner WHERE tipologia = '$tipologia' AND durata = '$durata'";
			else
				$sql = "SELECT id_tariffa from tariffe_abb WHERE tipologia = '$tipologia' AND durata = '$durata'";
			
			$result=mysql_query($sql, $this->conn) or die("Query failed");
			
			while ($row = mysql_fetch_array($result))
			{				
				$id_tariffa = $row["id_tariffa"];
				break;
			}	
					
			mysql_free_result($result);
			//mysql_close($this->conn);
			return $id_tariffa;
		}
		catch (Exception $e)
		{	
			$message  = 'Invalid query: ' . mysql_error() . "\n";
			$message .= 'Whole query: ' . $sql;
			mysql_free_result($result);
			mysql_close($this->conn);
			//die($message);
			header("Location: /admin/profile.php?id=".$user_id."&alert=26#tabs-4");					
			return 0;
		}	
	}
	public function GetDurataGG($tipologia, $durata, $tipo)
	{
		try
		{
			$durata_gg = 0;
			if($tipo=="Banner")
				$sql = "SELECT durata_gg from tariffe_banner WHERE tipologia = '$tipologia' AND durata = '$durata'";
			else
				$sql = "SELECT durata_gg from tariffe_abb WHERE tipologia = '$tipologia' AND durata = '$durata'";
			
			$result=mysql_query($sql, $this->conn) or die("Query failed");
			
			while ($row = mysql_fetch_array($result))
			{				
				$durata_gg = $row["durata_gg"];
				break;
			}	
					
			mysql_free_result($result);
			//mysql_close($this->conn);
			return $durata_gg;
		}
		catch (Exception $e)
		{	
			$message  = 'Invalid query: ' . mysql_error() . "\n";
			$message .= 'Whole query: ' . $sql;
			mysql_free_result($result);
			mysql_close($this->conn);
			//die($message);
			header("Location: /admin/profile.php?id=".$user_id."&alert=26#tabs-4");					
			return 0;
		}	
	}
	public function CalcoloCostoAbbonamento($id_tariffa, $sconto=0)
	{
		try
		{
			$costo = 0;
			$sql = "SELECT costo from tariffe_abb WHERE id_tariffa = $id_tariffa";
			
			$result=mysql_query($sql, $this->conn) or die("Query failed");
			
			if (mysql_num_rows($result)==1)
			{
				while ($row = mysql_fetch_array($result))
				{				
					$costo = $row["costo"];
					break;
				}				
			}
					
			mysql_free_result($result);
			if($sconto!="" && $sconto>0)
				$costo = $costo * (100-$sconto)/100; 
			//mysql_close($this->conn);
			return $costo;
		}
		catch (Exception $e)
		{	
			$message  = 'Invalid query: ' . mysql_error() . "\n";
			$message .= 'Whole query: ' . $sql;
			mysql_free_result($result);
			mysql_close($conn);
			//die($message);
			header("Location: /admin/profile.php?id=".$user_id."&alert=26#tabs-4");						
			return 0;
		}	
	}
	public function CalcoloCostoBanner($id_tariffa, $sconto=0)
	{
		try
		{
			$costo = 0;
			$sql = "SELECT costo from tariffe_banner WHERE id_tariffa = $id_tariffa";
			//die($sql);
			$result=mysql_query($sql, $this->conn) or die("Query failed");
			
			if (mysql_num_rows($result)==1)
			{
				while ($row = mysql_fetch_array($result))
				{				
					$costo = $row["costo"];
					break;
				}				
			}
					
			mysql_free_result($result);
			if($sconto!="" && $sconto>0)
				$costo = $costo * (100-$sconto)/100; 
			//mysql_close($this->conn);
			return $costo;
		}
		catch (Exception $e)
		{	
			$message  = 'Invalid query: ' . mysql_error() . "\n";
			$message .= 'Whole query: ' . $sql;
			mysql_free_result($result);
			mysql_close($conn);
			//die($message);
			header("Location: /admin/profile.php?id=".$user_id."&alert=26#tabs-4");				
			return 0;
		}	
	}
	
}
?>
